package com.woniu.shop.controller;


import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.ListShopAppraiseDTO;
import com.woniu.shop.entity.dto.ShopAppraiseDTO;
import com.woniu.shop.pojo.ShopAppraise;
import com.woniu.shop.service.IShopAppraiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 门店评价表 前端控制器
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/shop-appraise")
@Api(tags = "门店评价控制类")
public class ShopAppraiseController {

    @Autowired
    private IShopAppraiseService shopAppraiseService;

    @GetMapping("/list/appraise/{currentPage}/{pageSize}")
    @ApiOperation("分页查询门店评价")
    public Result<PageResult<ShopAppraise>> listShopAppraise(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody ListShopAppraiseDTO listShopAppraiseDTO){
        if(listShopAppraiseDTO.getCreatedTimeBegin() != null && listShopAppraiseDTO.getCreatedTimeEnd() != null){
            if(listShopAppraiseDTO.getCreatedTimeBegin().equals(listShopAppraiseDTO.getCreatedTimeEnd())){
                PageResult<ShopAppraise> appraiseList = shopAppraiseService.listShopAppraise(currentPage, pageSize, listShopAppraiseDTO);
                return Result.ok(appraiseList);
            }
            if(!listShopAppraiseDTO.getCreatedTimeBegin().before(listShopAppraiseDTO.getCreatedTimeEnd())){
                return Result.error(ResultCode.ERROR.getCode(), "结束时间不能早于开始时间");
            }
        }
        PageResult<ShopAppraise> appraiseList = shopAppraiseService.listShopAppraise(currentPage, pageSize, listShopAppraiseDTO);
        return Result.ok(appraiseList);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("查看评价详情|id")
    public Result<ShopAppraise> getById(@PathVariable String id){
        ShopAppraise shopAppraise = shopAppraiseService.getById(id);
        return Result.ok(shopAppraise);
    }

    @GetMapping("/reply/{id}")
    @ApiOperation("回复评价|id")
    public Result<Object> reply(@PathVariable Integer id, String replyContent){
        boolean status = shopAppraiseService.reply(id, replyContent);
        if(!status){
            return Result.error("原因可能是：评价已被删除或已经回复");
        }
        return Result.ok();
    }

    @PostMapping("/appraise")
    @ApiOperation("用户发表门店评价")
    public Result<Object> userAppraiseTheShop(@RequestBody ShopAppraiseDTO shopAppraiseDTO){
        if(1 > shopAppraiseDTO.getGrade() || 5 < shopAppraiseDTO.getGrade()){
            return Result.error("评价星级应为1~5星");
        }
        boolean status = shopAppraiseService.userAppraiseTheShop(shopAppraiseDTO);
        if(!status){
            return Result.error("发表失败，请稍后重试");
        }
        return Result.ok();
    }


}
